package Servlet;

import Service.RegisterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-09
 * Time: 21:45
 */
//注册申请表多操作(多删或多通)
@WebServlet("/RegisterProcessServlet")
public class RegisterProcessServlet extends HttpServlet {
    private RegisterService registerService = new RegisterService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        //获取要进行处理的registerId
        String[] registerIds = req.getParameterValues("registerIds");
        String action = req.getParameter("action");
        boolean ok = false;
        if(action.equals("approve")) {
            System.out.println("操作为批准申请");
            ok = registerService.registerPass(registerIds);
        } else if(action.equals("reject")) {
            System.out.println("操作为拒绝申请");
            ok = registerService.delete(registerIds);
        }

        if(ok) {
            req.setAttribute("status", "success");
            req.setAttribute("message", "操作成功!!!");
            req.setAttribute("redirectUrl", "RegisterListServlet?pageNo=1");
            req.setAttribute("waitSeconds", 2);
        } else {
            req.setAttribute("status", "error");
            req.setAttribute("message", "删除失败,请检查数据库");
            req.setAttribute("redirectUrl", "admin_index.jsp");
            req.setAttribute("waitSeconds", 3);
        }
        req.getRequestDispatcher("skip.jsp").forward(req,resp);
    }
}
